package org.admin.entity.dto;

import core.validate.Rule;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.admin.entity.BaseDto;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;

@Data
@EqualsAndHashCode(callSuper = true)
public class PermsDto extends BaseDto<PermsDto>{
    protected PermsDto inst(){ return this; }

    public PermsDto(){

        addRule(Rule.<String>NumberGroup(new HashMap<>() {{
            put("id", "接口力度ID");
            put("appId", "应用程序ID");
            put("groupId", "接口分组ID");
        }}, (rule, title) -> {
            rule.onPreCheck(value -> {
                if(value instanceof String && value.equals("")){
                    return null;
                }
                return value;
            }).min(rule.key.equals("id")? 0 : 1,  title + "格式不正确");
            // .integer(title + "无效")
        }).toArray(
                new Rule<?>[0])
        ).addRule(new Rule<?> [] {
                Rule.String("name").max(24, "接口名称过长"),
                Rule.String("detail").max(200, "接口描述过长"),
                Rule.String("route").max(200, "接口路由过长")
                        .regex("^/?[0-9a-zA-Z_-]+(/[0-9a-zA-Z_-]+)*$", "接口路由格式不正确"),
                Rule.String("method").in(new String[]{ "POST", "GET", "PUT", "DELETE" }, "接口请求方式不正确"),
                Rule.Number("power").in(new Integer[] { 1, 2, 3, 4, 5, 6, 7 }, "请选择正确的接口力度"),
                Rule.Number("current").min(1, "当前页数不正确"),
                    // .integer("无效的页数"),
                Rule.Number("size").onPreCheck(
                        value -> Objects.requireNonNullElse(value, 20)
                ).min(1, "当前数据条目不正确"),
                Rule.Array("ids").min(1, "未指定删除项"),
        });
    }

    private Integer id;
    private Integer appId;
    private Integer groupId;
    private String route;
    private String method;
    private Integer power;
    private String detail;
    private String name;

    private List<Integer> ids;
    private int current = 1;
    private int size = 20;
}